package System.machine;

import patterns.Status.MachineStatus;
import patterns.factory.FarmMachineFactory;
import patterns.proxy.FarmMachineProxy;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class FarmMachineManager {
    private Map<String, FarmMachine> machines = new HashMap<>();

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n=== 农机设备管理 ===");
            System.out.println("1. 添加新农机");
            System.out.println("2. 查看所有农机状态");
            System.out.println("3. 查看单个农机详情");
            System.out.println("4. 分配农机任务");
            System.out.println("5. 更新农机状态");
            System.out.println("6. 更新农机油量");
            System.out.println("7. 更新农机位置");
            System.out.println("0. 返回上级菜单");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addNewMachine(scanner);
                    break;
                case 2:
                    viewAllMachinesStatus();
                    break;
                case 3:
                    viewMachineDetails(scanner);
                    break;
                case 4:
                    assignMachineTask(scanner);
                    break;
                case 5:
                    updateMachineStatus(scanner);
                    break;
                case 6:
                    updateMachineFuel(scanner);
                    break;
                case 7:
                    updateMachineLocation(scanner);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private void addNewMachine(Scanner scanner) {
        System.out.println("选择农机类型: 1.播种机 2.喷药机 3.收割机");
        int type = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        if (machines.containsKey(machineId)) {
            System.out.println("错误: 该农机ID已存在！");
            return;
        }

        FarmMachineFactory factory = null;
        switch (type) {
            case 1:
                factory = new com.agricultural.system.patterns.factory.SeedingMachineFactory();
                break;
            case 2:
                factory = new com.agricultural.system.patterns.factory.SprayingMachineFactory();
                break;
            case 3:
                factory = new com.agricultural.system.patterns.factory.HarvestingMachineFactory();
                break;
            default:
                System.out.println("无效的农机类型！");
                return;
        }

        FarmMachine machine = factory.createMachine(machineId);
        // 使用代理模式包装农机对象
        FarmMachine machineProxy = new FarmMachineProxy(machine);
        machines.put(machineId, machineProxy);

        System.out.println("成功添加农机: " + machine.getMachineType() + " (ID: " + machineId + ")");
    }

    private void viewAllMachinesStatus() {
        if (machines.isEmpty()) {
            System.out.println("暂无农机设备！");
            return;
        }

        System.out.println("\n=== 所有农机状态 ===");
        for (FarmMachine machine : machines.values()) {
            System.out.printf("%s - %s: %s (%.1f%% 油量)%n",
                    machine.getMachineId(),
                    machine.getMachineType(),
                    machine.getCurrentStatus().getDescription(),
                    machine.getFuelLevel());
        }
    }

    private void viewMachineDetails(Scanner scanner) {
        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        FarmMachine machine = machines.get(machineId);
        if (machine == null) {
            System.out.println("错误: 未找到该农机！");
            return;
        }

        machine.displayStatus();
    }

    private void assignMachineTask(Scanner scanner) {
        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        FarmMachine machine = machines.get(machineId);
        if (machine == null) {
            System.out.println("错误: 未找到该农机！");
            return;
        }

        System.out.println("输入任务描述:");
        String task = scanner.nextLine();

        machine.assignTask(task);
    }

    private void updateMachineStatus(Scanner scanner) {
        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        FarmMachine machine = machines.get(machineId);
        if (machine == null) {
            System.out.println("错误: 未找到该农机！");
            return;
        }

        System.out.println("选择新状态:");
        System.out.println("1. 空闲");
        System.out.println("2. 作业中");
        System.out.println("3. 维修中");
        System.out.println("4. 离线");
        System.out.println("5. 低油量");

        int statusChoice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        MachineStatus newStatus = null;
        switch (statusChoice) {
            case 1:
                newStatus = MachineStatus.IDLE;
                break;
            case 2:
                newStatus = MachineStatus.WORKING;
                break;
            case 3:
                newStatus = MachineStatus.MAINTENANCE;
                break;
            case 4:
                newStatus = MachineStatus.OFFLINE;
                break;
            case 5:
                newStatus = MachineStatus.LOW_FUEL;
                break;
            default:
                System.out.println("无效的状态选择！");
                return;
        }

        machine.updateStatus(newStatus);
    }

    private void updateMachineFuel(Scanner scanner) {
        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        FarmMachine machine = machines.get(machineId);
        if (machine == null) {
            System.out.println("错误: 未找到该农机！");
            return;
        }

        System.out.println("输入新油量百分比:");
        double fuelLevel = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符

        machine.setFuelLevel(fuelLevel);
    }

    private void updateMachineLocation(Scanner scanner) {
        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        FarmMachine machine = machines.get(machineId);
        if (machine == null) {
            System.out.println("错误: 未找到该农机！");
            return;
        }

        System.out.println("输入新位置:");
        String location = scanner.nextLine();

        machine.setLocation(location);
    }

    public FarmMachine getMachine(String machineId) {
        return machines.get(machineId);
    }

}
